<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/detail.css">
    <link rel="stylesheet" href="../css/ui-base.css">
    <script src="../js/jquery-1.10.1.min.js"></script>
    <script src="../js/request.js"></script>
    <script src="../js/cookie.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        img {
            vertical-align: top;
        }

        .wrap {
            width: 1140px;
            margin: 0 auto;
            margin-top: 100px;
            position: relative;
            overflow: hidden;
            height: 600px;
        }

        .wrapBox {
            width: 400px;
            height: 400px;
            background-color: pink;
            position: relative;
        }

        .wrapBox img {
            width: 100%;
            height: 100%;
        }

        .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.6);
            position: absolute;
            left: 0;
            top: 0;

            display: none;
        }

        .showBigImg {
            width: 400px;
            height: 400px;
            float: left;
            background-color: #0000004d;
            overflow: hidden;
            position: absolute;
            left: 500px;
            top: 0;

            display: none;

        }

        .showBigImg img {
            position: absolute;
            /* z-index: -1; */
            width: 800px;
            height: 800px;
        }

        .switchList {
            width: 400px;
            margin-top: 20px;
            display: flex;
        }

        .switchList li {
            flex: 1;
            margin: 5px;
            border: 1px solid transparent;
        }

        .switchList .active {
            border-color: red;
        }

        .switchList li img {
            width: 100%;
        }

        .wrap .left {
            float: left;
        }

        .wrap .right {
            float: left;
            /* overflow: hidden; */
            width: 640px;
            margin-left: 100px;
            background-color: #6ff;
            min-height: 400px;
        }

        .wrap .right h2 {
            font-size: 18px;
            margin-bottom: 20px;
        }

        .wrap .right>div {
            margin-bottom: 20px;
        }

        .detailBox {
            width: 1140px;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div class="box">
        <a href="./login.html">你好,请登录</a>
        <a href="./register.html">注册</a>
    </div>
    <div class="wrap">
        <div class="left">
            <!-- <div class="wrapBox">
                <div class="shadow"></div>
                <img class="bigImg" src="../images/small.jpg" alt="">
            </div>
            <ul class="switchList">
                <li class="active"><img src="../images/small.jpg" bigImg="../images/big.jpg" alt=""></li>
                <li><img src="../images/girlsmall1.jpg" bigImg="../images/girlbig1.jpg" alt=""></li>
                <li><img src="../images/girlsmall2.jpg" bigImg="../images/girlbig2.jpg" alt=""></li>
                <li><img src="../images/girlsmall3.jpg" bigImg="../images/girlbig3.jpg" alt=""></li>
                <li><img src="../images/girlsmall4.jpg" bigImg="../images/girlbig4.jpg" alt=""></li>
            </ul>
            <div class="showBigImg">
                <img class="bigImg" src="../images/big.jpg" alt="">
            </div> -->
        </div>
        <div class="right">
            <!-- <h2>Apple iPhone 11 (A2223) 128GB 黑色 移动联通电信4G手机 双卡双待</h2>
            <div class="price">
                京 东 价:￥ <span>4999.00</span> 降价通知
            </div>
            <div class="buyNum">
                <span class="reduce">-</span>
                <input type="number" value="1">
                <span class="add">+</span>
            </div>
            <button id="addToCar">
                加入购物车 -->
            </button>
        </div>
    </div>
    <div class="detailBox">

    </div>
</body>
<script>

    var cookie = document.cookie;
    if (cookie) {
        var user = getCookie("logUser");
        if (user) {
            $(".box").html(`<a href="javascript:;">欢迎,${user}</a><button class="exit">退出</button>`);

            var button = document.getElementsByClassName("exit")[0];
            button.onclick = function () {
                setCookie("logUser", "", -1);
                location.reload();
            }
        }
    } else {
        var user = "";
    }

    var search = location.search;
    if (search) {   // 有商品id在查询
        var gid = search.split("=")[1];
        if (gid) {
            searchGoodsByGoodsId({ gid }).then(data => {
                console.log(data);

                var { goodId, goodsName, goodsPrice, smallPicList, bigPicList, goodsDetail } = data;


                // left 
                var leftHTML = "";
                var switchHTML = "";

                // right
                var rightHTML = "";

                leftHTML = `  <div class="wrapBox">
                        <div class="shadow"></div>
                        <img class="smallImg" src="${bigPicList[0]}" alt="">
                    </div>
                    <ul class="switchList">
                    </ul>
                    <div class="showBigImg">
                        <img class="bigImg" src="${bigPicList[0]}" alt="">
                    </div>`;

                smallPicList.forEach(function (smallImg, index) {
                    if (index <= 4) {
                        switchHTML += `<li class="${index == 0 ? 'active' : ''}"><img src="${smallImg}" bigImg="${bigPicList[index]}" alt=""></li>`
                    }
                })


                $(".left").html(leftHTML);
                $(".left .switchList").html(switchHTML);


                rightHTML += `<h2>${goodsName}</h2>
                <div class="price">
                    京 东 价:￥ <span>${goodsPrice}</span> 降价通知
                </div>
                <div class="buyNum">
                    <span class="reduce">-</span>
                    <input class="count-input" type="number" value="1">
                    <span class="add">+</span>
                </div>
                <button id="addToCar">
                    加入购物车
                </button>`;


                $(".right").html(rightHTML);

                $(".detailBox").html(goodsDetail);
            })

        }


        // switchList
        $(document).on("mouseenter", ".switchList li", function () {
            // console.log(this);

            $(this).addClass("active").siblings().removeClass("active");
            var bigSrc = $(this).find("img").attr("bigImg");

            $(".smallImg").prop("src", bigSrc);
            $(".bigImg").prop("src", bigSrc);
        })

        $(document).on("click", "#addToCar", function () {
            // console.log("判断用户是否登录");
            var user = getCookie("logUser");   // 
            if (user) {  // 用户登录
                console.log("加入购物车");

                // 加入购物车  =>   谁  买了什么东西  买了多少件 ?
                // 谁           =>   登录的用户  user
                // 买了什么东西  =>   goodsId()
                // 买了多少件    =>   $(".buyNum .count-input").val();


                //  0  已经被删除了(假删)
                //  1  假如购物车但是还没有购买
                //  2  买了还没有付钱
                //  3  已经购买正在发货 

                addToShoppingCar({
                    user,
                    gid,
                    buyNum: $(".buyNum .count-input").val(),
                }).then(result => {
                    console.log(result);
                    if (result.status) {
                        if (confirm("商品添加成功,是否进入购物车")) {
                            location.href = "./shoppingCar.html";
                        }
                    } else {
                        alert(result.msg);
                    }


                })








            } else {  // 用户没有登录   => 返回登录(记录当前页面的地址 returnUrl  )

                var url = encodeURIComponent(location.href);

                location.href = "./login.html?returnUrl=" + url;
            }
        })



    } else {
        location.href = "./goodsList.html";
    }




</script>

</html>